using Godot;
using Arcane.Source.Combat;

namespace Arcane.Source.Client.Extensions;
public static class ModifierExt
{
    public static readonly StringName TYPE = "type";
    public static readonly StringName OP = "op";
    public static readonly StringName VALUE = "value";

    public static bool TryCastModifier(this Godot.Collections.Dictionary dict, out Modifier modifier)
    {
        if (!dict.TryGetValue(TYPE, out var ty)) goto Fail;
        if (!dict.TryGetValue(OP, out var opStr)) goto Fail;
        if (!dict.TryGetValue(VALUE, out var val)) goto Fail;

        if (ty.AsString()?.TryParseModifierType(out var type) != true) goto Fail;
        if (opStr.AsString()?.TryParseModifierOp(out var op) != true) goto Fail;

        modifier = new Modifier
        {
            type = type,
            op = op,
            value = val.AsSingle(),
        };
        return true;

    Fail:
        modifier = null;
        return false;
    }
}